package com.ouc.oceanBase.service.impl;


import com.ouc.common.utils.TextUtils;
import com.ouc.oceanBase.domain.EddySet;
import com.ouc.oceanBase.mapper.EddySetMapper;
import com.ouc.oceanBase.service.EddySetService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * eddySet的服务层
 *
 * @author B504
 */
@Service
public class EddySetServiceImpl implements EddySetService {
  @Autowired
  EddySetMapper eddySetMapper;

  @Autowired
  SqlSessionFactory sqlSessionFactory;

  @Override
  public EddySet selectEddySetById(Integer id) {
    return eddySetMapper.selectEddySetById(id);
  }

  @Override
  public List<EddySet> selectEddySet(EddySet eddySet) {
    return eddySetMapper.selectEddySet(eddySet);
  }

  @Override
  public Integer insertEddySet(EddySet eddySet) {
    return eddySetMapper.insertEddySet(eddySet);
  }

  @Override
  public Integer updateEddySet(EddySet eddySet) {
    return eddySetMapper.updateEddySet(eddySet);
  }

  @Override
  public Integer deleteEddySetByEddyId(Integer eddyId) {
    return eddySetMapper.deleteEddySetByEddyId(eddyId);
  }

  @Override
  public Integer deleteEddySetByEddyIds(String[] eddSetId) {
    return eddySetMapper.deleteEddySetByEddyIds(eddSetId);
  }

  @Override
  public Integer deleteEddySetById(Integer id) {
    return eddySetMapper.deleteEddySetById(id);
  }

  @Override
  public Integer deleteEddySet() {
    return eddySetMapper.deleteEddySet();
  }

  @Override
  public void truncateTable() {
    eddySetMapper.truncateTable();
  }

  @Override
  public void insertByBach(List<EddySet> eddySetList) {
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    //	自动提交设置为false
    EddySetMapper mapper = sqlSession.getMapper(EddySetMapper.class);
    for (EddySet eddySet : eddySetList) {
      mapper.insertEddySet(eddySet);
    }

    sqlSession.commit();
    sqlSession.clearCache();
    sqlSession.close();
  }

  @Override
  public List<EddySet> getEddyFormTxt(String filePath) {
    List<EddySet> listEddy = new ArrayList<>();
    List<String> list = TextUtils.readTxt(filePath);
    for (String s : list) {
      String[] s1 = s.split("\t");
      for (int i = 0; i < s1.length; i++) {
        EddySet eddySet = new EddySet();

        eddySet.setEddyType(Integer.parseInt(s1[1]));
        eddySet.setEddyLon(Float.parseFloat(s1[6]));
        eddySet.setEddyLat(Float.parseFloat(s1[5]));
        eddySet.setEddyRadius(Float.parseFloat(s1[4]));
        listEddy.add(eddySet);
      }
    }
    return listEddy;
  }
}
